package Servlet;

import DAO.UserDAO;
import POJO.Response;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class ChangeInfo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        UserDAO userDao = new UserDAO();
        int userId = Integer.parseInt(req.getParameter("userId"));
        String perSign = req.getParameter("perSign");
        String regPhone = req.getParameter("regPhone");
        String regEmail = req.getParameter("regEmail");
        String regPhoto = req.getParameter("regPhoto");

        //System.out.println();
        resp.setContentType("text/json;charset=UtF-8");
        PrintWriter out = resp.getWriter();
        Response res;
        try{
            if(userDao.userIdExisted(userId)){
                userDao.changeUserInfo(userId,perSign,regPhone,regEmail,regPhoto);
                res = new Response(0, "修改成功", null);
            }else {
                res = new Response(1000,"用户不存在",null);
            }

        }catch(SQLException e){
            System.out.println(e);
            res = new Response(1001,"错误",null);
        }
        Gson gson = new Gson();
        String json = gson.toJson(res);

        out.println(json);
    }
}
